<template>
  <div>
    <div class="head">
      <div class="kemu">{{ subject_name }}</div>
    </div>
    <div class="classify">
      <div class="dotted" />
      <div class="classify_for" v-for="(item, index) in classifyList" :key="index">
        <div class="classify_div">
          <div class="classify_title">{{ item.name }}</div>
          <i class="dot" />
          <div class="classify_time">{{ item.desc }}</div>
        </div>
      </div>
    </div>

    <van-divider style="margin: 0px" />

    <van-tabs :swipe-threshold="1" :active="active" sticky style="padding-bottom: 170px">
      <van-tab v-for="(itme, index) in catesList" :key="index" :title="itme.name">
        <van-tabs :swipe-threshold="1" id="fenlei" :active="active1" sticky>
          <van-tab v-for="(desc, index1) in itme.descList" :key="index1" :title="desc.title">
            <div style="font-size: 14px; padding: 0 15px" v-html="desc.content"></div>
          </van-tab>
        </van-tabs>
      </van-tab>
    </van-tabs>

    <div class="bottom">
      <div class="headline">
        <span class="h_left">
          <van-icon :name="require('@/assets/images/headline_ico.png')" />
          <i class="ml5">其它科目报考指南</i>
        </span>
      </div>
      <div class="kemu" v-if="subjectList[0]">
        <van-button
          round
          type="default"
          v-for="(item, index) in subjectList[0].children"
          :key="index"
          @click="subClick(item)"
          v-show="index < 3"
          :class="subject_id == item.id ? 'active' : ''"
        >{{ item.name }}</van-button>
      </div>
      <div class="all" @click="showKong = true">
        <div>点击展开全部科目</div>
        <van-icon size="18" name="arrow-down" />
      </div>
    </div>

    <!--选择科目 -->
    <div class="subject_popup">
      <van-popup v-model="showKong" closeable position="bottom">
        <div v-for="(item, index) in subjectList" :key="index">
          <div class="headline">
            <img
              class="headline_ico"
              :src="require('@/assets/images/subject_' + item.id + '.png')"
              alt
            />
            <span class="headline_title">{{ item.name }}</span>
          </div>
          <div class="headline_two clearfix">
            <ul class="clearfix">
              <li
                :class="subject_id == sub.id ? 'active' : ''"
                v-for="(sub, index) in item.children"
                :key="index"
                @click="subClick(sub)"
              >
                <span class="headline_two_title">{{ sub.name }}</span>
              </li>
            </ul>
          </div>
        </div>
      </van-popup>
    </div>
  </div>
</template>

<script>
import { handleTree } from '@/utils'
import { getCates, getGuide } from '@/api/news/index'
import { getSubjectList } from '@/api/user'
export default {
  data() {
    return {
      active: 0,
      active1: 0,
      queryParams: {
        type: 'guide',
        subject_id: null
      },
      classifyList: [],
      catesList: [],
      guidesList: [],
      subject_id: this.$route.query.subject_id || '2',
      subject_name: '',
      showKong: false,
      subjectList: []
    }
  },

  computed: {},

  mounted() {
    document.title = '报考指南'
    this.getSubject()
    this.fetchList()
  },

  methods: {
    getSubject() {
      getSubjectList().then(res => {
        this.subjectList = handleTree(res.data)
        this.subjectList.forEach(j => {
          if (j.id == this.subject_id) {
            this.subject_name = j['name']
          } else {
            if (j.children) {
              j.children.forEach(b => {
                if (b.id == this.subject_id) {
                  this.subject_name = b['name']
                }
              })
            }
          }
        })
      })
    },
    // 获取首页数据
    async fetchList() {
      this.queryParams.subject_id = this.subject_id
      await getCates(this.queryParams).then(res => {
        this.catesList = res.data
      })
      await getGuide(this.queryParams).then(res => {
        this.classifyList = res.data.times
        this.guidesList = res.data.guides
      })
      await this.catesList.map(b => {
        this.guidesList.map(j => {
          if (j.id === b.id) {
            this.$set(b, 'descList', j.news)
          }
        })
      })
    },
    // 切换科目
    subClick(row) {
      // this.$router.push({ query: { subject_id: row.id } })
      // this.$route.query.subject_id = row.id
      this.subject_id = row.id
      this.subject_name = row.name
      this.showKong = false
      this.fetchList()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .van-tabs__wrap {
  border-bottom: 1px solid #ddd;
}
.head {
  display: flex;
  width: 100%;
  height: 110px;
  background: url('@/assets/images/register-guide.png');
  background-size: 100% 100%;
  .kemu {
    margin: 21px auto 0px;
    font-size: 19px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #ffffff;
  }
}
.classify {
  box-sizing: border-box;
  display: flex;
  width: 100%;
  height: 90px;
  padding: 0 20px;
  // background: #f3f6fd;
  overflow: hidden;
  white-space: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  .classify_for {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 25px;
  }
  .classify_div {
    margin-top: 10px;
    // width: 60px;
    height: 60px;
    // margin-top: 14px;
    text-align: center;
    // border: 1px solid #333;
  }
  .classify_icon {
    font-size: 16px;
  }
  .classify_title {
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 700;
    color: #333333;
    line-height: 24px;
  }
  .dot {
    display: block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: #8b9898;
    margin: 10px auto;
  }
  .dotted {
    width: 96%;
    height: 1px;
    border-bottom: 2px dotted #dddddd;
    position: absolute;
    left: 2%;
    top: 164px;
    z-index: 1;
  }
  .classify_time {
    font-size: 12px;
    font-family: Source Han Sans CN;
    font-weight: 00;
    color: #666666;
  }
  .arrow {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: 5px;
    font-size: 13px;
  }
}
.classify_for:last-child .arrow {
  display: none;
}

::v-deep #fenlei {
  margin-top: 15px;
  // padding: 0 15px;
  .van-tabs__wrap {
    border: none;
    height: 100%;
  }
  .van-tabs__line {
    display: none;
  }
  .van-sticky {
    padding: 0 15px;
  }
  .van-tab {
    flex: none;
    height: 30px;
    padding: 0 15px;
    margin-right: 15px;
    border: 1px solid #deeded;
    border-radius: 15px;
    .van-tab__text {
      font-size: 14px;
      font-weight: 500;
      color: #11a8a4;
    }
  }
  .van-tab--active {
    background: #009697;
    .van-tab__text {
      color: #ffffff;
    }
  }
}

.bottom {
  box-sizing: border-box;
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 150px;
  padding-top: 15px;
  background: #ffffff;
  box-shadow: 0px 0px 12px 0px #eaeaea;

  .kemu {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }
  .van-button {
    width: 30%;
    height: 35px;
    // margin-right: 3%;
    border: 1px solid #deeded;
    font-size: 14px;
    font-weight: 500;
    color: #11a8a4;
  }

  .all {
    margin-top: 15px;
    text-align: center;
    font-size: 12px;
    font-weight: 500;
    color: #999999;
    line-height: 20px;
  }
}
.active {
  background: #009697 !important;
  color: #ffffff !important;
}

::v-deep div::-webkit-scrollbar {
  width: 0px;
  height: 0px;
}

.subject_popup {
  .van-popup {
    height: 385px;
    padding-bottom: 50px;
    border-radius: 10px 10px 0px 0px;
  }
  .headline {
    margin-top: 22px;
    display: flex;
    span {
      display: table-cell;
    }
    .headline_ico {
      width: 25px;
      height: 25px;
      background: #e5f4f4;
      border-radius: 50%;
    }
    .headline_title {
      padding-left: 10px;
      font-size: 15px;
      font-weight: bold;
      color: #333333;
      line-height: 30px;
    }
  }

  .headline_two {
    padding: 0 15px;
    li {
      float: left;
      width: 48%;
      height: 38px;
      margin-top: 10px;
      line-height: 30px;
      text-align: center;
      background: #f6f6f6;
      border-radius: 19px;
      .headline_two_title {
        font-size: 14px;
        font-weight: 500;
        color: #333333;
      }
    }
    li:nth-of-type(odd) {
      float: left;
    }
    li:nth-of-type(even) {
      float: right;
    }
  }
  .active {
    background: #009697 !important;
    .headline_two_title {
      color: #ffffff !important;
    }
  }
}
</style>
